﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Model;

namespace LaKuaShop_TestDemo
{
    /// <summary>
    /// ChangeProduct.xaml 的交互逻辑
    /// </summary>
    public partial class ChangeProduct : Window
    {
        string Pid;
        bool isAdd;
        byte[] showimg;
        public ChangeProduct(bool isAdd)
        {
            InitializeComponent();
            this.isAdd = isAdd;
            BitmapImage bt = new BitmapImage(new Uri("/newlogo.png", UriKind.Relative));
            ProImg.Source = bt;
        }
        public ChangeProduct(bool isAdd,string Pid)
        {
            InitializeComponent();
            this.isAdd = isAdd;
            this.Pid = Pid;
            Init();
        }

        //刷新页面
        public void Init()
        {
            BLL.Product Pro = new BLL.Product();
            ProductInfo info = Pro.ShowProductInfo(Pid);
            Pname.Text = info.Pname; Pprice.Text = info.Pprice.ToString(); Psale.Text = info.Psale.ToString();
            Pnum.Text = info.Pnum.ToString();
            byte[] img = Pro.ShowProductImg(Pid);
            if (img != null)
            {
                MemoryStream stream = new MemoryStream(img);
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();//初始化
                bmp.StreamSource = stream;//设置源
                bmp.EndInit();//初始化结束
                ProImg.Source = bmp;
            }
            else
            {
                BitmapImage bt = new BitmapImage(new Uri("/newlogo.png", UriKind.Relative));
                ProImg.Source = bt;
            }
            switch (info.Ptag)
            {
                case "图书":
                    tushu.IsChecked = true;
                    break;
                case "服装":
                    fuzhuang.IsChecked = true;
                    break;
                case "饰品":
                    zhuangshi.IsChecked = true;
                    break;
                case "家居":
                    jiaju.IsChecked = true;
                    break;
                case "数码":
                    shuma.IsChecked = true;
                    break;
                case "食品":
                    shipin.IsChecked = true;
                    break;
            }
            Run r = new Run(info.Pintro);
            Paragraph para = new Paragraph();
            para.Inlines.Add(r);
            Pintro.Document.Blocks.Clear();
            Pintro.Document.Blocks.Add(para);
        }

        //保存按钮
        private void Save_click(object sender, RoutedEventArgs e)
        {
            TextRange textRange = new TextRange(Pintro.Document.ContentStart, Pintro.Document.ContentEnd);
            if (Pname.Text == "" || Pprice.Text == "" || Psale.Text == "" || Pnum.Text == "" || textRange.Text == "" ||
         (tushu.IsChecked == false && fuzhuang.IsChecked == false && zhuangshi.IsChecked == false
         && shipin.IsChecked == false && jiaju.IsChecked == false && shuma.IsChecked == false))
            {
                MessageBox.Show("请完善信息");
                return;
            }
            string name = Pname.Text;
            double price = double.Parse(Pprice.Text);
            double sale = double.Parse(Psale.Text);
            int num = int.Parse(Pnum.Text);
            string intro = textRange.Text;
            string tag = "";
            if (tushu.IsChecked == true) tag = "图书";
            if (fuzhuang.IsChecked == true) tag = "服装";
            if (zhuangshi.IsChecked == true) tag = "饰品";
            if (shipin.IsChecked == true) tag = "食品";
            if (jiaju.IsChecked == true) tag = "家居";
            if (shuma.IsChecked == true) tag = "数码";

            if (isAdd)
            {
                string uuid = System.Guid.NewGuid().ToString("N");
                ProductInfo productInfo = new ProductInfo(uuid, name, intro, DateTime.Now, num, sale, price, tag);
                BLL.Product Pro = new BLL.Product();
                if (showimg == null) { MessageBox.Show("请完善信息"); }
                else {
                    if (MessageBox.Show("确认添加?", "确认提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        if (Pro.AddProduct(productInfo) == 0 && Pro.AddProductImg(uuid, showimg) == 0)
                        {
                            MessageBox.Show("添加成功！");
                            Pname.Text = string.Empty; Psale.Text = string.Empty; Pprice.Text = string.Empty;
                            Pnum.Text = string.Empty; Pintro.Document.Blocks.Clear();
                            tushu.IsChecked = false; fuzhuang.IsChecked = false; zhuangshi.IsChecked = false;
                            jiaju.IsChecked = false; shuma.IsChecked = false; shipin.IsChecked = false;
                            BitmapImage bt = new BitmapImage(new Uri("/newlogo.png", UriKind.Relative));
                            ProImg.Source = bt;
                        }
                        else
                        {
                            MessageBox.Show("添加失败，系统错误");
                            return;
                        }
                    }
                }
                  
            }
            else
            {
                ProductInfo productInfo = new ProductInfo(Pid, name, intro, DateTime.Now, num, sale, price, tag);
                BLL.Product Pro = new BLL.Product();
                if (MessageBox.Show("确认修改?", "确认提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (Pro.UpdateProduct(Pid,productInfo) == 0&&(Pro.UpdateProductImg(Pid, showimg) == 0||showimg == null))
                    {
                        MessageBox.Show($"修改成功!");
                        Init();
                    }
                    else
                    {
                        MessageBox.Show("修改失败，系统错误");
                        return;
                    }
                }
                }
        }

        private void Close_Button(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //上传图片
        private void UpLoadImg(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif|All files(*.*)|*.*";

            if (dialog.ShowDialog() == true)
            {            
                BLL.Product Pro = new BLL.Product();
                showimg = File.ReadAllBytes(dialog.FileName);
                MemoryStream stream = new MemoryStream(showimg);
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();//初始化
                bmp.StreamSource = stream;//设置源
                bmp.EndInit();//初始化结束
                ProImg.Source = bmp;
            }
        }
    }
}
